package com.aaa.behind.controller;


import com.aaa.behind.service.IBAppealService;
import com.aaa.behind.service.IBBusinessInfoService;
import com.aaa.behind.service.IEEmpInfoService;
import com.aaa.behind.service.IEExamineService;
import com.aaa.entity.BAppeal;
import com.aaa.entity.EEmpInfo;
import com.aaa.entity.EExamine;
import com.aaa.util.PageInfo;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 商家申诉表 前端控制器
 * </p>
 *
 * @author whj
 * @since 2023-01-05
 */
@RestController
@RequestMapping("/b-appeal")
public class BAppealController {

    @Autowired
    private IBAppealService bAppealService;

    @Autowired
    private IEEmpInfoService empInfoService;

    @Autowired
    private IEExamineService examineService;

    @Autowired
    private IBBusinessInfoService businessInfoService;


    @PostMapping
    public Result findAll(@ModelAttribute PageInfo page, @ModelAttribute BAppeal bAppeal){
        IPage pageData = bAppealService.getPageData(page, bAppeal);
        return new Result(pageData);
    }

    @PostMapping("/tongguo")
    public Result updTongguo(BAppeal bAppeal,@ModelAttribute("telephone") String telephone){
        EEmpInfo eEmpInfo = empInfoService.selectName(telephone);
        String s = businessInfoService.selectId(bAppeal.getBusinessId());
        bAppeal.setBusinessId(s);
        bAppeal.setStatus(0);
        saveExamine(bAppeal,s,eEmpInfo);
        return new Result(bAppealService.updateById(bAppeal));
    }
    @PostMapping("/bohui")
    public Result updBohui(BAppeal bAppeal,@ModelAttribute("telephone") String telephone){
        EEmpInfo eEmpInfo = empInfoService.selectName(telephone);
        String s = businessInfoService.selectId(bAppeal.getBusinessId());
        bAppeal.setBusinessId(s);
        bAppeal.setStatus(1);
        saveExamine(bAppeal,s,eEmpInfo);

        return new Result(bAppealService.updateById(bAppeal));
    }

    public final boolean saveExamine(BAppeal bAppeal,String s,EEmpInfo eEmpInfo){
        EExamine eExamine = new EExamine();
        eExamine.setBid(s);
        eExamine.setCheckman(eEmpInfo.getId());
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        eExamine.setGmtCreate(format);
        eExamine.setMsg(bAppeal.getInfo());
        eExamine.setState(bAppeal.getStatus());
        eExamine.setIsDeleted(0);
        return examineService.saveOrUpdate(eExamine);
    }

}

